//
//  TitleView.m
//  Dispatch
//
//  Created by hyeyeon on 14. 1. 14..
//  Copyright (c) 2014년 hyeyeon. All rights reserved.
//

#import "TitleView.h"

@implementation TitleView
@synthesize delegate, currentKeyword;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        myRect = frame;
        appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
        // Initialization code
        [self makeObject];
        [self makeTwoLabel];
    }
    return self;
}

-(void)makeObject{
    
    //타이틀
    yellowImage = [[UIImageView alloc] initWithFrame:CGRectZero];
    yellowImage.image = [UIImage imageNamed:@"title_yellow"];
    [self addSubview:yellowImage];
    
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, mySize.width, 44)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textColor = ColorFromRGB(0x2a2829);
    [self addSubview:titleLabel];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, titleLabel.frame.origin.y+titleLabel.frame.size.height, mySize.width-20, 2)];
    line.backgroundColor = ColorFromRGB(0x2a2829);
    [self addSubview:line];
    
    
    //검색버튼
    searchButton = [[UIButton alloc] initWithFrame:CGRectMake(mySize.width-36-10, 0, 36,44)];
    [self addSubview:searchButton];
    [searchButton setImage:[UIImage imageNamed:@"btn_search"] forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //메뉴
    issueButton = [[UIButton alloc] initWithFrame:CGRectMake(0,
                                                             line.frame.origin.y+line.frame.size.height,
                                                             mySize.width/4.0f,
                                                             30)];
    [self addSubview:issueButton];
    [issueButton setTag:1];
    [issueButton setTitle:NSLocalizedString(@"Issue", @"") forState:UIControlStateNormal];
    [issueButton setTitleColor:ColorFromRGB(0x2a2829) forState:UIControlStateNormal];
    [issueButton setTitleColor:ColorFromRGB(0x49a1d9) forState:UIControlStateSelected];
    [issueButton addTarget:self action:@selector(menuButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    battleButton = [[UIButton alloc] initWithFrame:CGRectMake(issueButton.frame.origin.x+issueButton.frame.size.width,
                                                              issueButton.frame.origin.y,
                                                              issueButton.frame.size.width,
                                                              issueButton.frame.size.height)];
    [self addSubview:battleButton];
    [battleButton setTag:2];
    [battleButton setTitle:NSLocalizedString(@"Battle", @"") forState:UIControlStateNormal];
    [battleButton setTitleColor:ColorFromRGB(0x2a2829) forState:UIControlStateNormal];
    [battleButton setTitleColor:ColorFromRGB(0x49a1d9) forState:UIControlStateSelected];
    [battleButton addTarget:self action:@selector(menuButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //원래는 직찍이지만 시나리오 일정상 스타랭킹이로 바꿈
    captureButton = [[UIButton alloc] initWithFrame:CGRectMake(battleButton.frame.origin.x+battleButton.frame.size.width,
                                                               issueButton.frame.origin.y,
                                                               issueButton.frame.size.width,
                                                               issueButton.frame.size.height)];
    [self addSubview:captureButton];
    [captureButton setTag:3];
    [captureButton setTitle:NSLocalizedString(@"Star", @"") forState:UIControlStateNormal];
    [captureButton setTitleColor:ColorFromRGB(0x2a2829) forState:UIControlStateNormal];
    [captureButton setTitleColor:ColorFromRGB(0x49a1d9) forState:UIControlStateSelected];
    [captureButton addTarget:self action:@selector(starRankingButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    myButton = [[UIButton alloc] initWithFrame:CGRectMake(captureButton.frame.origin.x+captureButton.frame.size.width,
                                                          issueButton.frame.origin.y,
                                                          issueButton.frame.size.width,
                                                          issueButton.frame.size.height)];
    [self addSubview:myButton];
    [myButton setTag:4];
    [myButton setTitle:@"My" forState:UIControlStateNormal];
    [myButton setTitleColor:ColorFromRGB(0x2a2829) forState:UIControlStateNormal];
    [myButton setTitleColor:ColorFromRGB(0x49a1d9) forState:UIControlStateSelected];
    [myButton addTarget:self action:@selector(menuButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //스타랭킹보여주는 뷰
    UIView *starBGView = [[UIView alloc] initWithFrame:CGRectMake(0, issueButton.frame.origin.y+issueButton.frame.size.height, mySize.width, 30)];
    starBGView.backgroundColor = ColorFromRGB(0x615c65);
    [self addSubview:starBGView];
    
    //스타랭킹 버튼
    starRankingButton = [[UIButton alloc] initWithFrame:CGRectMake(10, issueButton.frame.origin.y+issueButton.frame.size.height, 70, 30)];
    [self addSubview:starRankingButton];
    [starRankingButton addTarget:self action:@selector(starRankingButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 19, 30)];
    [starRankingButton addSubview:iconImage];
    iconImage.image = [UIImage imageNamed:@"icon_star_ranking"];
    
    UILabel *buttonTitle = [[UILabel alloc] initWithFrame:CGRectMake(iconImage.frame.origin.x+iconImage.frame.size.width+3, 0, 50, 30)];
    [starRankingButton addSubview:buttonTitle];
    buttonTitle.backgroundColor = [UIColor clearColor];
    buttonTitle.textColor = ColorFromRGB(0xffffff);
    buttonTitle.text = NSLocalizedString(@"Star Ranking", @"");
    buttonTitle.font = [UIFont systemFontOfSize:13];
    [buttonTitle sizeToFit];
    buttonTitle.center = CGPointMake(buttonTitle.center.x, 15);
    
    starRankingButton.frame = CGRectMake(10, starRankingButton.frame.origin.y, iconImage.frame.size.width+6+buttonTitle.frame.size.width, 30);
    
    
    
    //랭킹 하나씩 보여주는 뷰
    rangkingView = [[UIView alloc] initWithFrame:CGRectMake(starRankingButton.frame.origin.x+starRankingButton.frame.size.width+13,
                                                            starRankingButton.frame.origin.y,
                                                            mySize.width-starRankingButton.frame.origin.x-starRankingButton.frame.size.width-13,
                                                            30)];
    [self addSubview:rangkingView];
    
    
    
    
    
}

#pragma mark - 메뉴

//스타랭킹
-(void)starRankingButtonAction:(id)sender{
    
    issueButton.selected = battleButton.selected = myButton.selected = NO;
    ViewController *viewContrller = (ViewController*)self.delegate;
    [viewContrller clickMenu:5];
}

//스타홈
-(void)upperButtonAction:(id)sender{
    
    issueButton.selected = battleButton.selected = myButton.selected = NO;
    ViewController *viewContrller = (ViewController*)self.delegate;
    [viewContrller clickMenu:5];
    
    NSString *starID = [[appDelegate.keywordsArray objectAtIndex:currentRankingIndex] objectForKey:@"star_id"];
    [viewContrller appearStarListToID:starID];
    
}

//검색
-(void)searchButtonAction:(id)sender{
    
    ViewController *viewContrller = (ViewController*)self.delegate;
    [viewContrller clickMenu:6];
    
}

//나머지
-(void)menuButtonAction:(id)sender{
    UIButton *button = (UIButton*)sender;
    
    issueButton.selected = battleButton.selected = captureButton.selected = myButton.selected = NO;
    button.selected = YES;
    
    ViewController *viewContrller = (ViewController*)self.delegate;
    [viewContrller clickMenu:button.tag];
}

#pragma mark - 타이틀 텍스트
-(void)setTitleText:(NSString *)titleText{
    titleLabel.text = titleText;
    [titleLabel sizeToFit];
    titleLabel.center = CGPointMake(mySize.width*0.5f, 22);
    yellowImage.frame = CGRectMake(titleLabel.frame.origin.x-15, 0, 33, 33);
}

-(void)selectMenuButton:(NSInteger)btnTag{
    
    switch (btnTag) {
        case 1:
            
            [self setTitleText:NSLocalizedString(@"Issue", @"")];
            issueButton.selected = YES;
            break;
            
        case 2:
            [self setTitleText:NSLocalizedString(@"Battle", @"")];
            battleButton.selected = YES;
            break;
            
        case 3:
            //직찍
            [self setTitleText:NSLocalizedString(@"Star Ranking", @"")];
            captureButton.selected = YES;
            break;
            
        case 4:
            [self setTitleText:@"My"];
            myButton.selected = YES;
            break;
            
        case 5:
            [self setTitleText:NSLocalizedString(@"Star Ranking", @"")];
            captureButton.selected = YES;
            break;
            
        case 6:
//            [self setTitleText:NSLocalizedString(@"Search", @"")];
            break;
            
        default:
            break;
    }
}


#pragma mark - 움직이는 랭킹뷰

-(void)makeTwoLabel{
    //위에
    upperButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, rangkingView.frame.size.width, rangkingView.frame.size.height)];
    upperButton.backgroundColor = ColorFromRGB(0x615c65);
    [rangkingView addSubview:upperButton];
    
    upperIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 16, 30)];
    [upperButton addSubview:upperIcon];
    
    upperLabel = [[UILabel alloc] initWithFrame:CGRectMake(upperIcon.frame.origin.x+upperIcon.frame.size.width+5, 0, upperButton.frame.size.width-16, 30)];
    upperLabel.backgroundColor = [UIColor clearColor];
    upperLabel.textColor = ColorFromRGB(0xffffff);
    upperLabel.font = [UIFont systemFontOfSize:13];
    [rangkingView addSubview:upperLabel];
    
    //밑에
    lowerButton = [[UIButton alloc] initWithFrame:CGRectMake(0,rangkingView.frame.size.height, rangkingView.frame.size.width, 0)];
    lowerButton.backgroundColor = ColorFromRGB(0x615c65);
    [lowerButton setTitleColor:ColorFromRGB(0xffffff) forState:UIControlStateNormal];
    [rangkingView addSubview:lowerButton];
    
    lowerIcon = [[UIImageView alloc] initWithFrame:upperIcon.frame];
    [lowerButton addSubview:lowerIcon];
    
    lowerLabel = [[UILabel alloc] initWithFrame:upperLabel.frame];
    lowerLabel.backgroundColor = [UIColor clearColor];
    lowerLabel.textColor = ColorFromRGB(0xffffff);
    lowerLabel.font = [UIFont systemFontOfSize:13];
    [lowerButton addSubview:lowerLabel];
    
    lowerButton.alpha = 0.0f;
    
    
    currentRankingIndex = 0;
    
    //그냥 스타로가자 : 바뀔수도있음
    [upperButton addTarget:self action:@selector(upperButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [lowerButton addTarget:self action:@selector(starRankingButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}


-(void)startRankingRolling{
    if (!rollingTimer) {
        if ([appDelegate.keywordsArray count] > 2) {
            
            rollingTimer = [NSTimer timerWithTimeInterval:4 target:self selector:@selector(nextRanking) userInfo:nil repeats:YES];
            
            NSString *upperText = [[appDelegate.keywordsArray objectAtIndex:0] objectForKey:@"rank_name"];
            NSString *lowerText = [[appDelegate.keywordsArray objectAtIndex:1] objectForKey:@"rank_name"];
            
            upperButton.tag = 0;
            lowerButton.tag = 1;
            upperIcon.image = [UIImage imageNamed:@"icon_n01"];
            lowerIcon.image = [UIImage imageNamed:@"icon_n02"];
            upperLabel.text = upperText;
            lowerLabel.text = lowerText;
            
            NSRunLoop *runLoop2  = [NSRunLoop currentRunLoop];
            [runLoop2 addTimer:rollingTimer forMode:NSDefaultRunLoopMode];
        }
    }
}

-(void)stopRankingRolling{
    if (rollingTimer) {
        
        [rollingTimer invalidate];
        rollingTimer = nil;
    }
}

-(void)nextRanking{
    
    currentRankingIndex++;
    
    
    currentRankingIndex = (currentRankingIndex >= [appDelegate.keywordsArray count]) ? 0 : currentRankingIndex;
    int nextIndex = (currentRankingIndex+1 >= [appDelegate.keywordsArray count]) ? 0 : currentRankingIndex+1;
    lowerButton.alpha = 0.0f;
    
    [UIView animateWithDuration:0.5f animations:^{
        lowerButton.frame = CGRectMake(0, 0, rangkingView.frame.size.width, rangkingView.frame.size.height);
        lowerButton.alpha = 1.0f;
    } completion:^(BOOL finished) {
        lowerButton.frame = CGRectMake(0,rangkingView.frame.size.height, rangkingView.frame.size.width, 0);
        NSString *upperText = [[appDelegate.keywordsArray objectAtIndex:currentRankingIndex] objectForKey:@"rank_name"];
        NSString *lowerText = [[appDelegate.keywordsArray objectAtIndex:nextIndex] objectForKey:@"rank_name"];
        
        upperButton.tag = currentRankingIndex;
        lowerButton.tag = nextIndex;
        upperLabel.text = upperText;
        lowerLabel.text = lowerText;
        upperIcon.image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_n%02d",currentRankingIndex+1]];
        lowerIcon.image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_n%02d",nextIndex+1]];
        
        
//        [upperButton setTitle:upperText forState:UIControlStateNormal];
//        [lowerButton setTitle:lowerText forState:UIControlStateNormal];
        lowerButton.alpha = 0.0f;
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


@end
